<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <Group @select="handleSelect" />
    <BasicTable @register="registerTable" class="overflow-hidden">
      <template #toolbar>
        <Authority :value="'conf.add'">
          <a-button type="primary" @click="handleCreate"> 新增 </a-button>
        </Authority>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction :actions="[
            {
              icon: 'clarity:note-edit-line',
              auth: 'conf.edit',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              auth: 'conf.del',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                placement: 'left',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]" />
        </template>
      </template>
    </BasicTable>
    <EditModal @register="registerModal" @success="handleSuccess" />
  </PageWrapper>
</template>
<script lang="ts" setup>
import { ref, computed } from 'vue';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { useModal } from '/@/components/Modal';
import { Authority } from '/@/components/Authority';
import { PageWrapper } from '/@/components/Page';
import EditModal from './EditModal.vue';
import Group from './Group.vue'
import { columns, searchFormSchema } from './data';
import { getConfPage, removConf } from '/@/api/config/conf';


const groupId = ref(0)
const env = ref('')
const searchinfo = computed(() => { return { groupId, env } })
const handleSelect = async (item) => {
  groupId.value = item.id
  env.value = item.env
  await reload()
}
const [registerModal, { openModal }] = useModal();
const [registerTable, { reload }] = useTable({
  title: '配置列表',
  api: getConfPage,
  columns,
  rowKey: 'id',
  immediate: false,
  searchInfo: searchinfo,
  formConfig: {
    labelWidth: 90,
    schemas: searchFormSchema,
  },
  useSearchForm: true,
  showTableSetting: true,
  bordered: true,
  showIndexColumn: false,
  actionColumn: {
    width: 100,
    title: '操作',
    dataIndex: 'action'
  },
});



function handleCreate() {
  openModal(true, {
    isUpdate: false,
    env
  });
}

function handleEdit(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
    env
  });
}

async function handleDelete(record: Recordable) {
  await removConf([record['id']]);
  reload();
}

function handleSuccess() {
  reload();
}
</script>